package com.apes.framework.plugin.soe.store.database.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.soe.metadata.BusinessField;
import com.apes.framework.plugin.soe.store.database.repository.BusinessFieldRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName BusinessFieldService
 * @Description TODO
 * @Author pengyb
 * @Date 2019/1/26 17:07
 * @Version 1.0
 **/
@Service("businessFieldService")
public class BusinessFieldService extends DomainService {
    @Autowired
    private BusinessFieldRepository businessFieldRepository;

    /**
     * 保存
     *
     * @param request
     * @return
     */
    public BusinessField create(SimpleRequest request) {
        BusinessField businessField = request.getO(BusinessField.class);
        checkData(businessField);
        return businessFieldRepository.saveAndFlush(businessField);
    }

    /**
     * 更新
     *
     * @param request
     * @return
     */
    public BusinessField update(SimpleRequest request) {
        BusinessField businessField = request.getO(BusinessField.class);
        checkData(businessField);
        return businessFieldRepository.saveAndFlush(businessField);
    }

    public void checkData(BusinessField businessField) {
        if (businessField.getBusinessFieldItemList().size() == 0) return;
        if(!businessField.getBusinessFieldItemList().stream().anyMatch(e->e.getCode().contains(".")))
            throw new RuntimeException("通用名不合格式，请联系开发人员");
    }
}
